<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区 -->
    <el-card class="box-card">
      <!-- 栅格系统 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 分类表格  -->
      <tree-table
        class="treeTable"
        ref="table"
        sum-text="sum"
        index-text="#"
        :data="catelist"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        border
        :show-row-hover="true"
      >
        <template slot="isok" scope="scope">
          <i v-if="scope.row.cat_deleted == false" class="el-icon-success" style="color:green"></i>
          <i v-else class="el-icon-error" style="color:red"></i>
        </template>
        <template slot="order" scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag size="mini" v-else type="warning">三级</el-tag>
        </template>
        <template slot="opt">
          <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
          <el-button size="mini" type="danger " icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 新增弹出框 -->
    <el-dialog
      title="提示"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateHandleClose"
    >
      <el-form ref="addCateFormRef" :model="addCateForm" :rules="addCateRules" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            clearable
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChanged"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addcate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        checkStrictly: 'true',
        expandTrigger: 'hover'
      },

      selectedKeys: [],
      // 父级的数据没有包含最后一项
      parentCateList: [],
      // 弹出框内的ipt的验证规则
      addCateRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 添加分类表单的数据
      addCateForm: {
        // 将要添加的分类的名称
        cat_name: '',
        // 父级分类的Id
        cat_pid: 0,
        // 分类的等级，默认要添加的是1级分类
        cat_level: 0
      },
      // 控制弹出框的显示与隐藏
      addCateDialogVisible: false,
      // 总数据条数
      total: 0,
      // 为table指定列的定义
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          template: 'order',
          type: 'template'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      // 商品分类的数据列表，默认为空
      catelist: [],
      // 查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      }
    }
  },
  created() {
    this.getCategoriesList()
    this.getparentCateList()
  },
  methods: {
    // 点击确认添加事件
    async addcate() {
      let { data: res } = await this.$axios.post('categories', this.addCateForm)
      console.log(res)
      if (res.meta.status !== 201) {
        return this.$Message.error('error' + res.meta.msg)
      }
      this.$Message.success('恭喜' + res.meta.msg)
      this.getCategoriesList()
      this.addCateDialogVisible = false
    },
    // 级联选择器的改变事件
    parentCateChanged() {
      console.log(this.selectedKeys)
      if (this.selectedKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectedKeys[
          this.selectedKeys.length - 1
        ]
        this.addCateForm.cat_level = this.selectedKeys.length
      } else {
        // 父级分类的Id
        this.addCateForm.cat_pid = 0
        // 为当前分类的等级赋值
        this.addCateForm.cat_level = 0
      }
    },
    // 获取父级数据
    async getparentCateList() {
      let { data: res } = await this.$axios.get('categories', {
        params: { type: 2 }
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$Message.error('获取数据失败')
      }
      this.parentCateList = res.data
    },

    // 弹框关闭事件
    addCateHandleClose() {
      this.$refs.addCateFormRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    },
    // 点击添加 弹出添加弹出框
    showAddCateDialog() {
      this.addCateDialogVisible = true
    },
    // 获取列表数据
    async getCategoriesList() {
      let { data: res } = await this.$axios.get('categories', {
        params: this.queryInfo
      })

      if (res.meta.status !== 200) {
        return
      }
      console.log(res)
      // // console.log(res.data)
      this.catelist = res.data.result
      // console.log(this.catelist)
      this.total = res.data.total
      // console.log(this.total)
    },
    handleSizeChange(res) {
      this.queryInfo.pagesize = res
      this.getCategoriesList()
    },
    handleCurrentChange(res) {
      this.queryInfo.pagenum = res
      this.getCategoriesList()
    }
  }
}
</script>

<style lang="less">
.treeTable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
